﻿using ApplicationLibrary;
using ApplicationLibrary.SqlConnector;

namespace Vintner.Masters.ModuleData
{
    #region DRDescriptions

    public class DRDescriptions : DataReaderUpdater
    {
        private SqlABParameter p1, p2;
        private DescriptionType descType;
        private string code;

        public DRDescriptions(DescriptionType descType)
            : this(descType, false)
        { }

        public DRDescriptions(DescriptionType descType, bool updater)
            : base(updater)
        {
            this.descType = descType;
        }

        protected override void CreateConnection()
        {
            myTable = VI_Descriptions.Name;

            QueryBuilder qB = new QueryBuilder();
            p1 = new SqlABParameter("@p50", VI_Descriptions.Type);
            p2 = new SqlABParameter("@p51", VI_Descriptions.Code);

            qB.AddSelect();
            qB.AddFrom(VI_Descriptions.Name);
            qB.AddCompare(VI_Descriptions.Type, p1);
            qB.AddCompare(VI_Descriptions.Code, p2);

            sqlCM = new SqlABCommand(qB.Query, sqlCN);
            sqlCM.Parameters.Add(p1);
            sqlCM.Parameters.Add(p2);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        public bool Find(string code)
        {
            this.code = code;
            return base.Find();
        }

        protected override void SetParameters()
        {
            p1.Value = descType.Int();
            p2.Value = code;
        }
    }

    #endregion

    #region RRDescriptions

    public class RRDescriptions : DataReaderUpdater
    {
        private SqlABParameter p1;
        private DescriptionType descType;

        public RRDescriptions(DescriptionType descType)
            : base(false)
        {
            this.descType = descType;
        }

        protected override void CreateConnection()
        {
            myTable = VI_Descriptions.Name;

            QueryBuilder qB = new QueryBuilder();
            p1 = new SqlABParameter("@p50", VI_Descriptions.Type);

            qB.AddSelect();
            qB.AddFrom(VI_Descriptions.Name);
            qB.AddCompare(VI_Descriptions.Type, p1);
            qB.AddOrder(VI_Descriptions.Code);

            sqlCM = new SqlABCommand(qB.Query, sqlCN);
            sqlCM.Parameters.Add(p1);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        protected override void SetParameters()
        {
            p1.Value = descType.Int();
        }
    }

    #endregion

    #region DRCustomer

    public class DRCustomer : DataReaderUpdater
    {
        private SqlABParameter p1;
        private string customer;

        public DRCustomer(bool updater)
            : base(updater)
        { }

        protected override void CreateConnection()
        {
            myTable = VI_Customers.Name;

            QueryBuilder qB = new QueryBuilder();
            p1 = new SqlABParameter("@p50", VI_Customers.Customer);

            qB.AddSelect();
            qB.AddFrom(VI_Customers.Name);
            qB.AddCompare(VI_Customers.Customer, p1);

            sqlCM = new SqlABCommand(qB.Query, sqlCN);
            sqlCM.Parameters.Add(p1);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        public bool Find(string customer)
        {
            this.customer = customer;
            return base.Find();
        }

        protected override void SetParameters()
        {
            p1.Value = customer;
        }
    }

    #endregion

    #region DRTaxCode

    public class DRTaxCode : DataReaderUpdater
    {
        private SqlABParameter p1;
        private string tax;

        public DRTaxCode(bool updater)
            : base(updater)
        { }

        public DRTaxCode()
            : base(false)
        { }

        protected override void CreateConnection()
        {
            myTable = VI_TaxCode.Name;

            QueryBuilder qB = new QueryBuilder();
            p1 = new SqlABParameter("@p50", VI_TaxCode.Code);

            qB.AddSelect();
            qB.AddFrom(VI_TaxCode.Name);
            qB.AddCompare(VI_TaxCode.Code, p1);

            sqlCM = new SqlABCommand(qB.Query, sqlCN);
            sqlCM.Parameters.Add(p1);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        public bool Find(string tax)
        {
            this.tax = tax;
            return base.Find();
        }

        protected override void SetParameters()
        {
            p1.Value = tax;
        }
    }

    #endregion

    #region RRTaxCode

    public class RRTaxCode : DataReaderUpdater
    {
        public RRTaxCode()
            : base(false)
        { }

        protected override void CreateConnection()
        {
            myTable = VI_TaxCode.Name;

            QueryBuilder qB = new QueryBuilder();

            qB.AddSelect();
            qB.AddFrom(VI_TaxCode.Name);
            qB.AddOrder(VI_TaxCode.Code);

            sqlCM = new SqlABCommand(qB.Query, sqlCN);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        protected override void SetParameters()
        {
        }
    }

    #endregion
}